package com.lm.mongo.controller;

import org.springframework.web.bind.annotation.*;
import com.lm.mongo.repository.UserRepository;
import com.lm.mongo.entry.User;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    private final UserRepository userRepository;
    public UserController(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @PostMapping("/save")
    public User save(@RequestBody User user) {
        return userRepository.save(user);
    }

    @GetMapping
    public List<User> findAll() {
        return userRepository.findAll();
    }

    @GetMapping("/search")
    public List<User> search(@RequestParam String name) {
        return userRepository.findByName(name);
    }

    @GetMapping("/age")
    public List<User> searchByAge(@RequestParam int min, @RequestParam int max) {
        return userRepository.findByAgeBetween(min, max);
    }
}